package upm.pb4agent.common;

import java.awt.Point;
import java.util.HashMap;
import java.util.Vector;

import jade.content.Concept;
import jade.content.ContentElement;
import jade.content.onto.basic.Action;
import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.util.leap.ArrayList;
import jade.util.leap.List;
import upm.pb4agent.ontology.PB4AgentOntology;
import upm.pb4agent.ontology.PickSeat;
import upm.pb4agent.ontology.RealRow;
import upm.pb4agent.ontology.SubscribeReferee;
import upm.pb4agent.team8.Common.PB4Utils;
import upm.pb4agent.team8.Common.PB4Utils.MsgTypes;
import upm.pb4agent.team8.Manager.RowerMgr;

class LocalVector
{
	public Point O ;
	public Point D;
	LocalVector( int Ox, int Oy)
	{
		O = new Point();
		D = new Point();
		O.x = Ox;
		O.y = Oy;
		D.x = O.x;
		D.y = D.y;
	}
	
   public double distance() 
   {
      int xDiff = O.x - D.x; 
      int yDiff = O.y - D.y; 
      return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
   }
}

public class Boat extends Agent
{
	private HashMap<AID, String> RowersList = new HashMap<AID,String>();
	LocalVector _position;
	
	public int GetOccupiedSeats()
	{
		return RowersList.size();
		
	}
	
	public AID GetSeatOwner(String seat)
	{
		AID result = null;
		for (AID key : RowersList.keySet())
		{
			if (RowersList.get(key) == seat)
				result = key;
		}
		return result;
	}
	
	protected void setup()
	{
		SetupInitialConfiguration();
		PB4Utils.TryRegisterAgent(this, Constant.BOAT+ Constant.TEAM_ID);
		PB4Utils.Console.WriteLine( getLocalName() + ": Enters the system");
	}

	private void SetupInitialConfiguration()
	{
		_position = new LocalVector(Integer.parseInt(Constant.TEAM_ID)*20 , 0);
	}
	
	public class RegisterRowersBehaviour extends CyclicBehaviour
	{
		private Agent _agent;
		public RegisterRowersBehaviour (Agent agent)
		{
			super(agent);
			_agent=agent;
		}
		
		@Override
		public void action()
		{       
			ACLMessage msg = receive(MessageTemplate.MatchProtocol(PB4AgentOntology.PICKSEAT));
			if (msg != null)
			{
				ContentElement content = PB4Utils.GetMessageContent(_agent, msg);		
				switch (msg.getPerformative()) 
				{
				case ACLMessage.INFORM:
					Concept action = ((Action)content).getAction();
					if (action instanceof PickSeat )
					{
						PickSeat pSeat= (PickSeat)action;
						String seat = pSeat.getSeatID().getSeatID();
						
						//If more one requirement for a seat arrives, only one is take into account.
						if (GetSeatOwner(seat)!=null)
						{
							RowersList.put((AID)msg.getSender(),seat);
							PB4Utils.Console.WriteLine(msg.getSender().getName() + " picked seat \""+ seat + "\"");
						}
					}
					break;
				}
			}
		}
	}	

	public class CalculateBoatLocationBehaviour extends CyclicBehaviour
	{
		private Agent _agent;
		public CalculateBoatLocationBehaviour (Agent agent)
		{
			super(agent);
			_agent=agent;
		}
		
		@Override
		public void action()
		{       
			ACLMessage msg = receive(MessageTemplate.MatchProtocol(PB4AgentOntology.REALROW));
			if (msg != null)
			{		
				switch (msg.getPerformative()) 
				{
					case ACLMessage.INFORM:
						ContentElement content = PB4Utils.GetMessageContent(_agent, msg);
						Concept action = ((Action)content).getAction();
						if (action instanceof RealRow )
						{
							RealRow row= (RealRow)action;
							PB4Utils.Console.WriteLine("REcieve RealRow " + row.getX() + ","+ row.getY());
						}
						break;
				}
			}
		}
	}
}
